package com.serge.smartdacha;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;

class SwitchFragmentTabListener implements ActionBar.TabListener {

    private Context context;

    private Class<? extends Fragment> fragmentClass;

    private Fragment fragment;

    SwitchFragmentTabListener(Context context, Class<? extends Fragment> fragmentClass) {
        this.context = context;
        this.fragmentClass = fragmentClass;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        if (fragment == null) {
            fragment = Fragment.instantiate(context, fragmentClass.getName());
            fragmentTransaction.replace(android.R.id.content, fragment);
        } else {
            fragmentTransaction.attach(fragment);
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        if (fragment != null) {
            fragmentTransaction.detach(fragment);
        }
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }
}
